package com.ailesson.video.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import com.ailesson.video.entity.TeacherEntity;
import com.ailesson.video.mapper.TeacherMapper;
import com.ailesson.video.model.reply.TeacherReply;
import com.ailesson.video.model.req.TeacherQueryReq;
import com.ailesson.video.model.req.TeacherReq;
import com.ailesson.video.service.TeacherService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherEntity> implements TeacherService {

    @Override
    public PageResult queryPage(TeacherQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<TeacherEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<TeacherEntity>()
                       .likeRight(StringUtils.hasText(params.getName()),TeacherEntity::getName,params.getName())
                       .eq(params.getLevel() != null,TeacherEntity::getLevel,params.getLevel())
                       .between(params.getJoinDateBegin() != null || params.getJoinDateEnd() != null,
                               TeacherEntity::getJoinDate,params.getJoinDateBegin(),params.getJoinDateEnd())
                       .orderByDesc(TeacherEntity::getId)
       );
       List<TeacherReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               TeacherReply reply = new TeacherReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

    @Override
    public TeacherReply info(Long id) {
        TeacherEntity entity = this.getById(id);
        if (entity != null){
            TeacherReply reply = new TeacherReply();
            BeanUtils.copyProperties(entity, reply);
            return reply;
        }
        return null;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void update(TeacherReq teacher) {
        if (teacher.getId() == null || teacher.getId() <= 0){
            throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
        }
        TeacherEntity entity = this.getById(teacher.getId());
        if (entity == null){
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
        }
        BeanUtils.copyProperties(teacher, entity);
        this.updateById(entity);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Long add(TeacherReq teacher) {
        teacher.setId(null);
        TeacherEntity entity = new TeacherEntity();
        BeanUtils.copyProperties(teacher, entity);
        this.save(entity);
        return entity.getId();
    }

}